﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.Generic;
using PPFinance.Common;
namespace PPFinance.Common
{
    public class FileAction : IDisposable
    {

        public FileAction()
        {

        }
        /// <summary>
        /// 将 Stream 转成 byte[]
        /// </summary>
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>
        /// 将 byte[] 转成 Stream
        /// </summary>
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }

        public void Save(Stream stream, string fileName, string directoryName = "", bool autoCreate = true, bool overrideWrite = true)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                String filePath;
                filePath = System.IO.Path.Combine(directoryName, fileName);
                if (autoCreate)
                {
                    if (directoryName != "")
                    {
                        this.CreateDirectory(directoryName);
                    }
                }
                if (!overrideWrite)
                {
                    if (isoStore.FileExists(filePath))
                    {
                        return;
                    }
                }
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isoStore))
                {
                    byte[] content = new byte[stream.Length];
                    stream.Read(content, 0, content.Length);
                    isoStream.Write(content, 0, content.Length);
                    isoStream.Flush();
                }
            }

        }
        public bool ExistsDirectoryPath(string directoryPath)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return file.DirectoryExists(directoryPath);
            }
        }
        public bool ExistsFilePath(string filePath)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                return file.FileExists(filePath);
            }
        }
        public byte[] GetData(string filePath)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.FileExists(filePath))
                {
                    return null;
                }
                using (IsolatedStorageFileStream isoStream = isoStore.OpenFile(filePath, FileMode.Open))
                {
                    return StreamToBytes(isoStream);

                }
            }
        }
        public List<string> GetRootDirectory()
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string[] directories = file.GetDirectoryNames("*");
                return directories.ToList();
            }
        }
        public void CreateDirectory(string directoryName, string parentDirectory = "", bool overrideWrite = false)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                var dirPath = System.IO.Path.Combine(parentDirectory, directoryName);
                if (file.DirectoryExists(dirPath))
                {
                    if (overrideWrite)
                    {
                        file.DeleteDirectory(dirPath);
                    }
                    else
                    {
                        return;
                    }
                }
                file.CreateDirectory(dirPath);
            }
        }
        public List<string> GetFiles(string directoryPath)
        {
            List<string> l = new List<string>();
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (file.DirectoryExists(directoryPath))
                {
                    l.AddRange(file.GetFileNames(System.IO.Path.Combine(directoryPath, "*.*")).ToList());
                }
            }
            return l;
        }
        public void Dispose()
        {

        }
    }
}
